home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 10165 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  4.1 KB

  1. Path: yasc115.watson.ibm.com!dnsmith
  2. From: David N. Smith <dnsmith@watson.ibm.com>
  3. Newsgroups: comp.lang.misc,comp.lang.c,comp.lang.pl1
  4. Subject: Re: GOTO controversy
  5. Date: 15 Mar 1996 19:38:08 GMT
  6. Organization: IBM T J Watson Research Center
  7. Distribution: world
  8. Message-ID: <4icgv0$121g@watnews2.watson.ibm.com>
  9. References: <4hhdcu$43p@due.unit.no> <4i6u62$sof@hoho.quake.net> <8080.imc@comlab.ox.ac.uk> <3147F456.50F0@simi.is>
  10. NNTP-Posting-Host: yasc115.watson.ibm.com
  11. Mime-Version: 1.0
  12. Content-Type: text/plain; charset=ISO-8859-1
  13. Content-Transfer-Encoding: 8bit
  14. X-Newsreader: Nuntius 2.0.4_68K
  15. X-XXMessage-ID: <AD6F28101502E00F@yasc115.watson.ibm.com>
  16. X-XXDate: Fri, 15 Mar 1996 18:58:40 GMT
  17.  
  18. In article <4hl8mt$4po@newshost.cyberramp.net> John Noland,
  19. sinan@cyberramp.net writes:
  20. >>Example for a good use of goto:
  21. >>---                    ????
  22. >>------------------------------------------------------------------------------
  23. >>   HEV    hev1, hev2, hev3;     /* Event semaphores */
  24. >>   HMTX   hmtx;                 /* Mutex semaphore  */ 
  25. >>   void  *ptr;              
  26. >
  27. >>   if (!DosCreateEventSem(0, &hev1, 0, FALSE))
  28. >>       goto hev1_failed;
  29. >>
  30. >>   if (!DosCreateEventSem(0, &hev2, 0, FALSE))
  31. >>       goto hev2_failed;
  32. >
  33. >>   if (!DosCreateEventSem(0, &hev3, 0, FALSE))
  34. >>       goto hev3_failed;
  35. >>
  36. >>   if (!DosCreateMutexSem(0, &hmtx, 0, FALSE))
  37. >>       goto hmtx_failed;
  38. >>
  39. >>   if ((ptr = malloc(SOME_SIZE)) == NULL)
  40. >>       goto malloc_failed;
  41. >>
  42. >>   /* Do some stuff here */
  43. >>   return TRUE; /* We did okay */
  44. >>
  45. >>malloc_failed:
  46. >>   DosCloseMutexSem(hmtx);
  47. >>hmtx_failed:
  48. >>   DosCloseEventSem(hev3);
  49. >>hev3_failed:
  50. >>   DosCloseEventSem(hev2);
  51. >>hev2_failed:
  52. >>   DosCloseEventSem(hev1);
  53. >>hev1_failed:
  54. >>   return FALSE;
  55. >
  56.  
  57. One can introduce a flag and a subroutine. When the flag is non-zero
  58. something has failed. The rightmost 4 bits indicate what was allocated
  59. that needs to be released. Of course, in a real implementation, #defines
  60. would be needed for the bits. (My C is rusty; hope I haven't written
  61. anything too stupid.)
  62.  
  63. {
  64.    HEV    hev1, hev2, hev3;     /* Event semaphores */
  65.    HMTX   hmtx;                 /* Mutex semaphore  */ 
  66.    void  *ptr; 
  67.    int result = 0;              
  68.  
  69.    result = createStuff( &hev1, &hev2, &hev3, &htmx );
  70.    
  71.    if( !result )
  72.        if ((ptr = malloc(SOME_SIZE)) == NULL)
  73.            result = 0x100F;
  74.  
  75.    if( !result ) {
  76.        if( result & 0x01 ) DosCloseEventSem(hev1);
  77.        if( result & 0x02 ) DosCloseEventSem(hev2);
  78.        if( result & 0x04 ) DosCloseEventSem(hev3);
  79.        if( result & 0x08 ) DosCloseMutexSem(hmtx);
  80.        return FALSE;
  81.        }
  82.  
  83.    /* Do some stuff here */
  84.    return TRUE; /* We did okay */
  85. }
  86.  
  87.  
  88. int createStuff( HEV *hev1, HEV *hev2, HEV *hev3, HMTX *htmx ) 
  89. {
  90.    if (!DosCreateEventSem(0, hev1, 0, FALSE))  return 0x1000;
  91.    if (!DosCreateEventSem(0, hev2, 0, FALSE))  return 0x1001;
  92.    if (!DosCreateEventSem(0, hev3, 0, FALSE))  return 0x1003;
  93.    if (!DosCreateMutexSem(0, hmtx, 0, FALSE))  return 0x1007;
  94.    return 0;
  95. }
  96.  
  97. Now, is this better? It all depends upon how one defines 'better', I
  98. guess. Such cascaded allocations are always a bit of a mess, and the
  99. solution with GOTOs is clear and clean, provided it is isolated within a
  100. subroutine so that this is ALL the code, and one can readily see the
  101. scope of the GOTOs.
  102.  
  103. On the other hand, I've spent a lot of years writing code in a language
  104. that doesn't have a goto at all and I sure haven't missed them, even
  105. though I started out programming 30 years ago using FORTRAN IV and
  106. Assembler, in which one cannot do anything without a GOTO. I wrote huge
  107. amounts of C code a few years ago without a single GOTO and didn't miss
  108. them then either, and think my code was better without them. 
  109.  
  110. However, I still think that clean code is far more important that how
  111. well that code follows some arbitrary set of rules, even good rules.
  112.  
  113. Dave
  114.  
  115. __________________________________
  116. David N. Smith
  117. dnsmith@dnsmith.tjwslip.ny.k12.net (Preferred)
  118. dnsmith@watson.ibm.com
  119. 70167.2274@compuserve.com
  120. IBM T J Watson Research Center
  121. Hawthorne, NY
  122. __________________________________
  123. Any opinions or recommendations
  124. herein are those of the author  
  125. and not of his employer.
  126.